<?php
namespace App\Http\Controllers;

use App\Http\Logic\PledgeLogic;
use App\Http\Requests\Pledge\CancelOrderRequest;
use App\Http\Requests\Pledge\GetOrderInfoRequest;
use App\Http\Requests\Pledge\GetOrderListRequest;
use App\Http\Requests\Pledge\GetPledgeInfoRequest;
use App\Http\Requests\Pledge\GetRedeemPledgeInfoRequest;
use App\Http\Requests\Pledge\MakePledgeOrderRequest;
use App\Http\Requests\Pledge\RedeemPledgeOrderRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class PledgeController extends BaseController
{
    public function getLists(Request $request) : JsonResponse
    {
        $list = PledgeLogic::indexLists();

        return $this->success($list);
    }

    public function getListsByCoin(Request $request) : JsonResponse
    {
        $list = PledgeLogic::getListsByCoin();

        return $this->success($list);
    }

    public function getPledgeInfo(GetPledgeInfoRequest $request) : JsonResponse
    {
        $id = $request->get('id');

        $list = PledgeLogic::getInfo($id);

        return $this->success($list);
    }

    public function createPledgeOrder(MakePledgeOrderRequest $request) : JsonResponse
    {
        $rule_id = $request->get('rule_id');

        $pledge_num = $request->get('pledge_num');

        $pay_password = $request->get('pay_password');

        if(!Hash::check($pay_password, auth()->user()->pay_password)) {
            return $this->error(__('auth.password_error'));
        }

        $user_id = auth() -> user() -> id;

        $res = PledgeLogic::createOrder($user_id, $rule_id, $pledge_num);

        if($res === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        return $this->success($res);
    }

    public function getOrderList(GetOrderListRequest $request) : JsonResponse
    {
        $pageNo = $request->get('pageNo') ?? 1;

        $pageSize = $request->get('pageSize') ?? 15;

        $status = $request->get('status') ?? "";

        $user_id = auth() -> user() -> id;

        $list = PledgeLogic::getOrderList($user_id, $status, $pageNo, $pageSize);

        return $this->success($list);
    }

    public function getOrderInfo(GetOrderInfoRequest $request) : JsonResponse
    {
        $id = $request->get('order_id');

        $data = PledgeLogic::getOrderInfo($id);

        if($data === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        return $this->success($data);
    }

    public function cancelPledgeOrder(CancelOrderRequest $request) : JsonResponse
    {
        $id = $request->get('order_id');

        $data = PledgeLogic::cancelOrder($id);

        if($data === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        return $this->success($data);
    }

    public function getRedeemPledgeInfo(GetRedeemPledgeInfoRequest $request) : JsonResponse
    {
        $id = $request->get('order_id');

        $data = PledgeLogic::getRedeemPledgeInfo($id);

        if($data === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        return $this->success($data);
    }

    public function redeemPledgeOrder(RedeemPledgeOrderRequest $request) : JsonResponse
    {
        $id = $request->get('order_id');

        $pay_password = $request->get('pay_password');

        if(!Hash::check($pay_password, auth()->user()->pay_password)) {
            return $this->error(__('auth.password_error'));
        }

        $redeem_info = PledgeLogic::getRedeemPledgeInfo($id);

        if($redeem_info === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        if(!$redeem_info['need_ask']) {
            $redeem_info['redeem_fee'] = 0;
        }

        $data = PledgeLogic::redeemPledgeOrder($id, $redeem_info);

        if($data === false) {
            return $this->error(PledgeLogic::getErrorMessage());
        }

        return $this->success($data);
    }
}
